package com.upoint.sellorderservice.message;

import com.augmentum.sellproductserive.common.transfer.ProductInfoInput;
import com.upoint.sellorderservice.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @author jsbintask@gmail.com
 * @date 2019/2/21 16:44
 */
@Component
@Slf4j
public class ProductMsgReceiver {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RabbitListener(bindings = @QueueBinding(exchange = @Exchange(Constants.PRODUCT_MSG_EXCHANGE),
            value = @Queue(Constants.DECREASE_STOCK),
            key = Constants.DECREASE_ROUTEING_KEY))
    public void handleProductDecreaseStockMsg(ProductInfoInput productInfoInput) {
        log.info("received product decrease msg: " + productInfoInput);

        stringRedisTemplate.opsForValue().set(String.format(Constants.REDIS_PRODUCT_DECREASE_STOCK_PREFIX, productInfoInput.getProductId()),
                String.valueOf(productInfoInput.getProductQuantity()));
    }

    @RabbitListener(bindings = @QueueBinding(exchange = @Exchange(Constants.PRODUCT_MSG_EXCHANGE),
            value = @Queue(Constants.INCREASE_STOCK),
            key = Constants.INCREASE_ROUTEING_KEY))
    public void handleProductIncreaseStockMsg(ProductInfoInput productInfoInput) {
        log.info("received product increase msg: " + productInfoInput);
    }
}
